
#include "mainwindow.h"
#include "ui_mainwindow.h"




//初始化已有记录
void MainWindow::ReadId()
{

    QSqlQuery Query;
    QString Select("select 标识码 from List");
    Query.exec(Select);
    while(Query.next())
        IdList.append(Query.value(0).toString());

}

//设置当前行
void MainWindow::ListInit(int num)
{
    ui->LegacyList->setCurrentRow(num);
}







//刷新记录函数
void MainWindow::RenewList(int num)
{

    QSqlQuery Query;
    ui->LegacyList->clear();
    for(int i=0;i<IdList.size();i++)
    {

        QString Select=QString("select 圣遗物类型, 圣遗物主词条, 等级 from List where 标识码 = '%1'").arg(IdList.at(i));
        Query.exec(Select);

        Query.next();

        QString Output=Query.value(0).toString()+" "+Query.value(1).toString()+" "+QString::number(Query.value(2).toUInt())+"级";

        ui->LegacyList->addItem(Output);
        ListInit(num);
    }



}
//窗口构造函数
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    CreateDB();
    ReadId();

}
//窗口析构函数
MainWindow::~MainWindow()
{
    DeleDB();
    delete ui;
}
//生成记录按钮
void MainWindow::on_LegacyGen_clicked()
{
    IdList.append(GenerateInit());
    RenewList();
    ListInit(IdList.count()-1);
    int Crrntl=ui->LegacyList->currentRow();
    ui->LegacyList->itemClicked(ui->LegacyList->item(Crrntl));
}
//刷新记录按钮
void MainWindow::on_renew_clicked()
{
    RenewList();
}
//删除记录按钮
void MainWindow::on_DeleteOne_clicked()
{
    if(ui->LegacyList->count()!=0)
    {
        int CrrntLn = ui->LegacyList->currentRow();
        QString PrcssStr= IdList.at(CrrntLn);
        Dele(PrcssStr);
        IdList.removeAt(CrrntLn);
        if(CrrntLn==IdList.size())
            CrrntLn--;
        RenewList(CrrntLn);
        //使得删除后信息会随时更新
        if(ui->LegacyList->count()>0)
        {
            int Crrntl=ui->LegacyList->currentRow();
            ui->LegacyList->itemClicked(ui->LegacyList->item(Crrntl));
        }

    }else{
        QMessageBox::information(this,"警告","请先更新或者生成记录！");
    }


}
//可变的副词条信息显示
void MainWindow::on_LegacyList_itemClicked(QListWidgetItem *item)
{
    QSqlQuery Query;
    QString Sub1,Sub2,Sub3,Sub4;
    float Val1,Val2,Val3,Val4;
    QString Select=QString("select 圣遗物副词条1, 圣遗物副词条2, 圣遗物副词条3,圣遗物副词条4,副词条数值1, 副词条数值2, 副词条数值3,副词条数值4,等级 from List where 标识码 = '%1'").arg(IdList.at(ui->LegacyList->currentRow()));
    QString Output;
    Query.exec(Select);
    Query.next();
    Sub1=" "+Query.value(0).toString()+" ";
    Sub2=" "+Query.value(1).toString()+" ";
    Sub3=" "+Query.value(2).toString()+" ";
    Sub4=" "+Query.value(3).toString()+" ";
    Val1=Query.value(4).toFloat();
    Val2=Query.value(5).toFloat();
    Val3=Query.value(6).toFloat();
    Val4=Query.value(7).toFloat();
    //最大等级为20级
    if(Query.value(8).toUInt()==20)
    {
        ui->UpGrd->setEnabled(false);
    }else
    {
        ui->UpGrd->setEnabled(true);
    }
    if(Val4!=0)
    {
        Output=Sub1+QString::number(Val1,'f',1)+"\n"+Sub2+QString::number(Val2,'f',1)+"\n"+Sub3+QString::number(Val3,'f',1)+"\n"+Sub4+QString::number(Val4,'f',1);
    }else
    {
        Output=Sub1+QString::number(Val1,'f',1)+"\n"+Sub2+QString::number(Val2,'f',1)+"\n"+Sub3+QString::number(Val3,'f',1);
    }
    ui->SubInfo->setText(Output);

}

//升级按钮
void MainWindow::on_UpGrd_clicked()
{

    if(ui->LegacyList->count()!=0)
    {
        QString ID=IdList.at(ui->LegacyList->currentRow());

        Upgrade(ID);
        int Crrntl=ui->LegacyList->currentRow();

        RenewList(Crrntl);
        ui->LegacyList->itemClicked(ui->LegacyList->item(Crrntl));
    }else{
        QMessageBox::information(this,"警告","请先更新或者生成记录！");
    }

}

